<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<title></title>
		<link rel="stylesheet" href="../plugins/layui/css/layui.css" media="all" />
		<link rel="stylesheet" href="../css/global.css" media="all">
		<link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
		<link rel="stylesheet" href="../css/table.css" />
	</head>

	<body>
		<div class="admin-main">
			<!--<blockquote class="layui-elem-quote">
				<a href="javascript:;" class="layui-btn layui-btn-small layui-btn-danger" id="delete-excel"><i class="layui-icon"></i> 删除</a>
			</blockquote>-->
			
			<fieldset class="layui-elem-field">
				<legend>模板列表</legend>
				<div class="layui-field-box">
					<table class="site-table table-hover">
						<thead>
							<tr>
								<th><input type="checkbox" id="selected-all"></th>
								<!--<th>ID</th>-->
								<th>表名称</th>
								<th>模板文件名</th>
								<th>创建时间</th>
								<th>操作</th>
							</tr>
						</thead>
						<tbody class="jqList">
							
						</tbody>
					</table>

				</div>
			</fieldset>
			<div class="admin-table-page">
				<div id="page" class="page">
				</div>
			</div>
		</div>
		<script type="text/javascript" src="../plugins/layui/layui.js"></script>
		<script>
			layui.config({
				base: '../plugins/layui/modules/'
			});

			layui.use(['icheck', 'laypage', 'layer', 'form', 'layedit', 'laydate'], function() {
				var $ = layui.jquery,
					laypage = layui.laypage,
					layer = parent.layer === undefined ? layui.layer : parent.layer;
				
				var DownLoadFile = function (options) {
					var config=$.extend(true,{method:'post'},options);var $iframe=$('<iframe id="down-file-iframe" />');var $form=$('<form target="down-file-iframe" method="'+config.method+'" />');$form.attr('action',config.url);for(var key in config.data){$form.append('<input type="hidden" name="'+key+'" value="'+config.data[key]+'" />')};$iframe.append($form);$(document.body).append($iframe);$form[0].submit();$iframe.remove();
				}
				
				$('input').iCheck({
					checkboxClass: 'icheckbox_flat-green'
				});

				//page
				//每页条数
				var num = 10;
				//当前页数
				var page = 1;
				//静态发布页根地址
				var baseUrl = 'http://61.155.85.77:8999';
				$.get("/getExcelCount",function(data){
					if(data.success){
						var pages = data.data['count(*)'];
						laypage({
							cont: 'page',
							pages: Math.ceil(pages/num),
							jump: function(obj, first) {
								//全选取消
								$('#selected-all').iCheck('uncheck');
								//得到了当前页，用于向服务端请求对应数据
								var curr = obj.curr;
								window.reloadPage = function(curr,num){
									$.get("/getExcelListPage?page="+(curr-1)*num+'&num='+num,function(data2){
										if(data2.success){
											var arr = data2.data;
											var list = "";
											$.each(arr, function(i,e) {
												var temp = [];
												temp.push("<tr>");
												//console.log(e);
												temp.push("<td><input type=\"checkbox\" name=\"delIds[]\" value=\""+e.id+"\"></td>");
												temp.push("<td>"+e.name+"</td>");
												//temp.push("<td>"+e.pinyin+"</td>");
												temp.push("<td>"+e['excel_real_name']+"</td>");
												temp.push("<td>"+getdate(e.create_time)+"</td>");
												temp.push("<td>\
												<a href=\"javascript:;\" data-id=\""+e.id+"\" data-sheet=\'"+e['excel_sheet']+"\' title=\""+e.name+"\" class=\"layui-btn layui-btn-normal layui-btn-mini jqShow\">查看</a>\
												<a href=\"javascript:;\" data-id=\""+e.id+"\" data-file=\""+e['excel_file']+"\" data-real=\""+e['excel_real_name']+"\" class=\"layui-btn layui-btn-normal layui-btn-mini jqDownload\">下载</a>\
												<a href=\"javascript:;\" data-id=\""+e.id+"\" data-tblname=\""+e.pinyin+"\" data-file=\""+e['excel_file']+"\" data-opt=\"del\" class=\"layui-btn layui-btn-danger layui-btn-mini jqDelete\">删除</a>\
												</td>");
												
												temp.push("</tr>");
												list += temp.join('');
											});
											$('.jqList').empty().append(list);
											//重新渲染checkbox样式
											$('.jqList input[type=checkbox]').iCheck({checkboxClass:'icheckbox_flat-green'});
											bindTrEvent();
										}
									})
								}
								reloadPage(curr, num);
								window.reloadDbList = function(){
									reloadPage(curr, num);
								}
								parent.window.reloadDbList = function(){
									reloadPage(curr, num);
								}
							}
						});
					}
				})
				function bindTrEvent(){
					//全选
					$('.site-table tbody tr').off('click').on('click', function(event) {
						var $this = $(this);
						var $input = $this.children('td').eq(0).find('input');
						$input.on('ifChecked', function(e) {
							$this.css('background-color', '#EEEEEE');
						});
						$input.on('ifUnchecked', function(e) {
							$this.removeAttr('style');
						});
						$input.iCheck('toggle');
					}).find('input').each(function() {
						var $this = $(this);
						$this.on('ifChecked', function(e) {
							$this.parents('tr').css('background-color', '#EEEEEE');
						});
						$this.on('ifUnchecked', function(e) {
							$this.parents('tr').removeAttr('style');
						});
					});
					$('.jqShow').off('click').on('click',function(e){
						var $obj = $(this);
						e.preventDefault();
						e.stopPropagation();
						var sheet = $obj.attr("data-sheet");
						layer.open({
							title:$obj.attr("title"),
							type:2,
							content:'./excel/excel_show.html',
							btn:['关闭'],
							area:['600px','400px'],
							maxmin:true,
							success:function(o,i){
								//console.log(JSON.parse(sheet));
								var $fm = o.find("iframe");
								var t = setInterval(function(){
									if(typeof($fm[0].contentWindow.loadExcelData)=='function'){
										clearInterval(t);
										$fm[0].contentWindow.loadExcelData(JSON.parse(sheet));
									}
								}, 50);
							},
							cancel: function(index) {
								layer.close(index);
							}
						});
					})
					$('.jqDownload').off('click').on('click',function(e){
						var $obj = $(this);
						e.preventDefault();
						e.stopPropagation();
						var file = $obj.attr("data-file");
						var real = $obj.attr("data-real");
						//console.log(file,real);
						DownLoadFile({
							url:baseUrl+'/file_download',
							data:{file:file,real:real},
							method:'post'
						});
					})
					//删除
					$('.jqDelete').off('click').on('click',function(e){
						e.preventDefault();
						e.stopPropagation();
						var $obj = $(this);
						var id = $obj.attr("data-id");
						var tblname = $obj.attr("data-tblname");
						var file = $obj.attr("data-file");
						layer.confirm('确认删除？',{title:'提示'},function(index){
							$.post(baseUrl+'/excel_delete', {id:id,tableName:tblname,file:file}, function(back){
								if(back.success){
									layer.close(index);
									window.reloadDbList();
								}else{
									layer.alert(back.error, {title:'警告'});
								}
							})
						})
					})
					$('#delete-excel').off('click').on('click',function(e){
						e.preventDefault();
						e.stopPropagation();
						var $obj = $('input[name="delIds[]"]:checked');
						if($obj.length>0){
							var ids = [];
							$.each($obj, function(i,e) {
								ids.push(parseInt($(e).val()));
							});
							layer.confirm('确认删除？',{title:'提示'},function(index){
								$.post(baseUrl+'/excel_delete_muti',{ids:ids},function(back){
									if(back.success){
										layer.close(index);
										window.reloadDbList();
									}else{
										layer.alert(back.error, {title:'警告'});
									}
								})
							})
						}else{
							layer.msg('无选中数据');
						}
					})
				}
				
				$('#selected-all').on('ifChanged', function(event) {
					var $input = $('.site-table tbody tr td').find('input');
					$input.iCheck(event.currentTarget.checked ? 'check' : 'uncheck');
				});
			});
		</script>
	</body>

</html>